function listTxt() {
  // 对日期进行处理
  function formatDate(a) {
    let day = Math.ceil(parseInt(new Date() - new Date(a) / 86400000));
    return day + "天前";
  }
  // 发送请求
  $.ajax({
    type: "get",
    url: "/admin/comments",
    success: function (res) {
      if (res.status === 1) return $("tbody").html("");
      console.log(res);
      let str = "";
      res.data.forEach((item) => {
        let timer = formatDate(item.cdate);
        str += `
          <tr>
          <td>${item.id}</td>
          <td>${item.uname}</td>
          <td>${item.content}</td>
          <td>${item.count}</td>
          <td>
          <button data-id="${item.id}" type="button" class="layui-btn layui-btn-xs layui-btn-danger delete"> 删除
          </button>
          </td>
        </tr>
          `;
      });
      $("tbody").html(str);
    },
  });
}
listTxt();

// 点击删除
$("tbody").on("click", 'button:contains("删除")', function () {
  let id = $(this).data("id");
  layer.confirm("确定要删除吗?", function (index) {
    // console.log(id)
    $.ajax({
      type: "DELETE",
      url: `/admin/comments/${id}`,
      success: function (res) {
        if (res.status === 0) {
          layer.msg(
            "删除成功",
            {
              icon: 1,
              time: 1000, //2秒关闭
            },
            function () {
              layer.close(index);
              rendercomment();
            }
          );
        }
      },
    });
  });
});
